/**
 * Created By zhangguoqing 2016/6/29
 * 模块 删除 查询 编辑
 * */
var currentId;
var moduleTable;

$(document).ready(function(){
	var url = getRoot() + "/module/toAjaxFindModule";
	moduleTable = $("#module-table-container").ltsTable({
        url: url,
        templateId: 'module-table'
    });
	moduleTable.post({},1);
	
	LTS.colFormatter.optFormat = function (v, row) {
    	return '<a href="javascript:;" class="update-btn"><span class="label label-success" style="font-size: 14px"><i class="fa fa-edit"></i>编辑</span><span class="hidden lts-data">' + JSON.stringify(row) + '</span></a>&nbsp;' +
        '<a href="javascript:;" class="remove-btn"><span class="label label-primary" style="background-color: #DD6B55;font-size: 14px"><i class="fa fa-trash-o"></i>删除</span></a>&nbsp;';
    }
	
	clickRemoveButton();
	confirmClickDelButton();
	updateClickButton();
	confirmUpateClickButton();
	clickSearchButton();
});

/**
 * 点击删除按钮
 * */
function clickRemoveButton(){
	$(document).on("click",".remove-btn",function(){
		currentId = getCurrentId($(this));
		$("#remove-tip-modal").modal("show");
	});
}

/**
 *点击确认删除按钮 
 **/
function confirmClickDelButton(){
	$(document).on("click","#confirm-remove-btn",function(){
		$("#cancel-remove-btn").trigger("click");

        var params = {};
        params['id'] = parseInt($.trim(currentId));
        var url = getUrlBySuffix("/module/remove");

        $.postHttp(params, url, function (json) {
            if (json && json.success) {
                swal("成功", json['message'], "success");
                moduleTable.post({},pageNum);
                $("#remove-tip-modal").modal("hide");
            } else if (json) {
                swal("失败", json['message'], 'error');
            } else {
                swal("失败", "未能从服务器获取正确信息", 'error');
            }
        });
        currentId = "";
	});
}

/**
 * 编辑数据按钮
 * */
function updateClickButton(){
	$(document).on("click",".update-btn",function(){
        var moduleJson = $(this).find("span.lts-data").html();
        moduleJson = $.trim(moduleJson);
        var module = JSON.parse(moduleJson);
        $.each($('#editForm').parent().find(".form-control"), function () {
            var name = $(this).attr("name");
            if(isNull(name)){
                return true;
            }
            name = $.trim(name);
            var value = module[name];
            $(this).val(value);
        });
        $("#module-edit-modal").modal("show");
    });
}

/**
 * 点击编辑确认按钮
 * */
function confirmUpateClickButton(){
	$(document).on("click","#edit-btn",function(){
		var params = {};
        $.each($('#editForm').parent().find(".form-control"), function () {
            var name = $(this).attr("name");
            if(isNull(name)){
                return true;
            }
            var value = $(this).val();
            if(!isNull(value)){
                value = $.trim(value);
                params[name] = value;
            }
        });
        var url = getRoot() + "/module/update";
        $.postHttp(params,url,function(json){
            if (json && json.success) {
                swal("成功",json['message'],"success");
                $("#module-edit-modal").modal("hide");
                moduleTable.post({},pageNum);
                //$("#searchBtn").trigger("click");
            } else {
                if (json) {
                    swal('失败',json['message'], 'error');
                }
            }
        });
	});
}

/**
 * 点击搜索
 * 
 */
function clickSearchButton(){
	$(document).on("click", "#searchBtn", function () {
        var params = {};
        $.each($('#module-query-form').find(".form-control"), function () {
            var name = $(this).attr("name");
            if(isNull(name)){
                return true;
            }
            var value = $(this).val();
            if(!isNull(value)){
                value = $.trim(value);
                params[name] = value;
            }
        });
        moduleTable.post(params, 1);
    });
}